<?php
/**
 * 添加系统用户管理和权限管理菜单
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 添加系统用户管理和权限管理菜单 ===\n\n";
    
    // 检查是否已存在权限管理菜单
    $stmt = $pdo->query("SELECT id, title FROM system_menu WHERE title = '权限管理' AND pid = 0");
    $parentMenu = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($parentMenu) {
        echo "✓ 已存在一级菜单：权限管理 (ID:{$parentMenu['id']})\n";
        $pid = $parentMenu['id'];
    } else {
        // 插入一级菜单：权限管理
        $stmt = $pdo->prepare("INSERT INTO system_menu (pid, title, icon, node, url, params, target, sort, status, create_at) VALUES (0, '权限管理', 'layui-icon layui-icon-auz', '', '#', '', '_self', 40, 1, NOW())");
        $stmt->execute();
        $pid = $pdo->lastInsertId();
        echo "✓ 插入一级菜单：权限管理 (ID:{$pid})\n";
    }
    
    // 检查是否已存在系统权限管理菜单
    $stmt = $pdo->prepare("SELECT id FROM system_menu WHERE node = 'admin/auth/index'");
    $stmt->execute();
    if ($stmt->fetch()) {
        echo "✓ 已存在：系统权限管理菜单\n";
    } else {
        // 插入二级菜单：系统权限管理
        $stmt = $pdo->prepare("INSERT INTO system_menu (pid, title, icon, node, url, params, target, sort, status, create_at) VALUES (?, '系统权限管理', 'layui-icon layui-icon-vercode', 'admin/auth/index', '', '', '_self', 10, 1, NOW())");
        $stmt->execute([$pid]);
        echo "✓ 插入二级菜单：系统权限管理 (ID:{$pdo->lastInsertId()})\n";
    }
    
    // 检查是否已存在系统用户管理菜单
    $stmt = $pdo->prepare("SELECT id FROM system_menu WHERE node = 'admin/user/index'");
    $stmt->execute();
    if ($stmt->fetch()) {
        echo "✓ 已存在：系统用户管理菜单\n";
    } else {
        // 插入二级菜单：系统用户管理
        $stmt = $pdo->prepare("INSERT INTO system_menu (pid, title, icon, node, url, params, target, sort, status, create_at) VALUES (?, '系统用户管理', 'layui-icon layui-icon-username', 'admin/user/index', '', '', '_self', 20, 1, NOW())");
        $stmt->execute([$pid]);
        echo "✓ 插入二级菜单：系统用户管理 (ID:{$pdo->lastInsertId()})\n";
    }
    
    echo "\n=== 完成！===\n";
    echo "请刷新后台页面（Ctrl+F5）或重新登录查看菜单\n\n";
    
    // 显示当前所有一级菜单
    echo "=== 当前所有一级菜单 ===\n";
    $stmt = $pdo->query("SELECT id, title, sort, status FROM system_menu WHERE pid = 0 ORDER BY sort DESC, id ASC");
    $menus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($menus as $menu) {
        $status = $menu['status'] == 1 ? '启用' : '禁用';
        echo "ID:{$menu['id']} | {$menu['title']} | 排序:{$menu['sort']} | {$status}\n";
        
        // 显示二级菜单
        $stmt2 = $pdo->prepare("SELECT id, title, node FROM system_menu WHERE pid = ? AND status = 1 ORDER BY sort DESC, id ASC");
        $stmt2->execute([$menu['id']]);
        $subMenus = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        foreach ($subMenus as $sub) {
            echo "  └─ {$sub['title']} ({$sub['node']})\n";
        }
    }
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

